#include<iostream>
using namespace std;

template<typename T>
class MyVector
{
public:
    MyVector(int size = 10)  :length(size),current_pos(0)
    {
        data = new int[current_pos];       
    }

    ~MyVector()
    {
        clear();
    }

    // 访问
    T& at(int index) const;
    T& front() const;
    T& back() const;
    T& operator[](int index);

    // 容量
    bool empty() const;
    int size() const;
    int capacity() const;

    // 修改
    void clear();
    void insert(int index, const T& d);
    void remove(int index);
    void push_back(const T& d);
    void pop_back();

private:
    T* data;
    int length;
    int current_pos;
};

//访问索引
template <typename T>
T& MyVector<T>::at(int index) const
{       
    return data[index];
}
template <typename T>
T& MyVector<T>::front() const
{             
    return data[0]; 
}
template <typename T>
T& MyVector<T>::back() const
{              
    return data[current_pos]; 
}
template <typename T>
T& MyVector<T>::operator[](int index) 
{    
    return data[index];   
}

//容量
template <typename T>
bool MyVector<T>::empty() const 
{
	return current_pos==0;
}
template <typename T>
int MyVector<T>::size() const
{
    return current_pos;
}
template <typename T>
int MyVector<T>::capacity() const
{
    return length-current_pos;
}

//修改
template <typename T>
void MyVector<T>::clear() 
{
	if (data == nullptr) 
		return;
    delete[] data;
	data = nullptr;
}
template <typename T>
void MyVector<T>::insert(int index, const T& d)
{
      
    for(int i=length-1; i>=index; --i)
    {
        data[i+1] = data[i];
    }
    data[index] = d;
    ++current_pos;
}
template <typename T>
void MyVector<T>::remove(int index)
{
    int i=current_pos;
    while(i>=0)
    {
        if(data[i]==index)
        {
            for(int j=i; j<=current_pos; ++j)
            {
                data[j] =data[j+1];
            }
            --current_pos;
        }
        i--;
    }
}
template <typename T>
void MyVector<T>::push_back(const T& d)
{
    data[current_pos++] = d;
}
template <typename T>
void MyVector<T>::pop_back()
{
    current_pos--;
}

int main()
{
    MyVector<int> v1;
    for(int i=1; i<6; i++)
    {
        v1.push_back(i);
    }
    cout<< v1.size()<<endl;
    cout<<"pop_back前："<< v1.back()<<"和"<< v1.front()<<endl;
    v1.pop_back();
    cout<<"pop_back后："<< v1.back()<<"和"<< v1.front()<<endl;
    cout<<"容器中的第四个元素："<<v1.at(3)<<endl;
    v1.insert(3,7);
    cout<<"插入后容器中的第四个元素："<< v1.at(3)<<endl;
    v1.remove(7);
    cout<<v1.at(3)<<endl;
    v1.clear();
    return 0;
}
